package com.sofwin.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.system.dto.DeptDto;
import com.sofwin.system.mapper.SysDeptMapper;
import com.sofwin.system.pojo.SysDept;
import com.sofwin.system.pojo.SysDeptExample;
import com.sofwin.system.service.DeptService;
import com.sofwin.system.vo.DeptTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @packageName: com.sofwin.system.service.impl
 * @user: andyliu
 * @date: 2021/7/16 9:56
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private SysDeptMapper mapper;
    @Override
    public PageInfo<SysDept> queryByPage(SysDept dept, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<SysDept> depts = queryDepts(dept);
        return new PageInfo<>(depts);
    }

    @Override
    public List<SysDept> queryDepts(SysDept dept) {
        SysDeptExample ex = new SysDeptExample();
        SysDeptExample.Criteria criteria = ex.createCriteria();
        if(dept!=null){
            if(dept.getDeptName()!=null && !"".equals(dept.getDeptName())){
                criteria.andDeptNameEqualTo(dept.getDeptName());
            }
            if(dept.getStatus()!=null && dept.getStatus()!=-1){
                criteria.andStatusEqualTo(dept.getStatus());
            }
        }
        List<SysDept> depts = mapper.selectByExample(ex);
        return depts;
    }

    @Override
    public boolean saveOrUpdate(SysDept dept) {
        if(dept.getDeptId()==null) {
            return mapper.insertSelective(dept) == 1 ? true : false;
        }
        return mapper.updateByPrimaryKeySelective(dept)==1?true:false;
    }

    @Override
    public List<DeptTreeVO> queryTreeData() {
        return mapper.selectDepts(null);
    }

    @Override
    public DeptDto queryById(Integer id) {
        return mapper.selectDeptDto(id);
    }
}
